package client;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.FilePath;

import com.google.gson.Gson;

import Dao.FoodsDao;
import Dao.Impl.FoodsDaoImpl;
import entity.Foods;


public class FoodServlet extends BaseServlet {

	private static final long serialVersionUID = 1L;


	public void explorerFoods(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		FoodsDao dao = new FoodsDaoImpl();
		try {
			List<Foods> explorer = dao.queryFood();
			for (Foods food : explorer) {
				String url = FilePath.imageUrl(request, food.getFood_picAdd());
				food.setFood_picAdd(url);
			}
			Gson gson = new Gson();
			String responseStr = gson.toJson(explorer);
			PrintWriter pw = response.getWriter();
			pw.write(responseStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(FilePath.imagePath(this.getServletContext()));
		
	}


	public void getFoodsWithTypeAndClass(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		String food_type = request.getParameter("food_type");
		String food_class = request.getParameter("food_class");
		if(food_type != null && food_type.isEmpty()) {
			food_type = null;
		}
		if(food_class != null && food_class.isEmpty()) {
			food_class = null;
		}
		FoodsDao dao = new FoodsDaoImpl();
		try {
			List<Foods> explorer = dao.queryByTypeAndClass(food_type, food_class);
			for (Foods food : explorer) {
				String url = FilePath.imageUrl(request, food.getFood_picAdd());
				food.setFood_picAdd(url);
			}
			Gson gson = new Gson();
			String responseStr = gson.toJson(explorer);
			PrintWriter pw = response.getWriter();
			pw.write(responseStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void allTypes(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	}

}
